package server

import (
	"MyWeb/internal/config"
	"MyWeb/internal/dao/mysql"
	"MyWeb/internal/dao/redis"
	"MyWeb/internal/global"
	"MyWeb/internal/pkg/logger"
	"MyWeb/internal/pkg/snowflake"
	"MyWeb/internal/routers"
)

func PrintMsg(Error error) {
	if Error != nil {
		panic(Error)
	}
}

// Init 注入依赖 global用来解耦，降低复杂度。
func Init() {
	// 加载配置文件
	config.InitConfig(global.Cfg)
	// 加载日志库
	PrintMsg(logger.InitLogger(global.Cfg, &global.Log))
	global.Log.Info("Log init successfully")
	// 加载mysql
	PrintMsg(mysql.InitMysqlDb(global.Cfg, &global.Db))
	global.Log.Info("Mysql init successfully")
	// 加载 redis
	PrintMsg(redis.InitRedisDb(global.Cfg, &global.Rdb))
	global.Log.Info("Redis init successfully")
	// 生成雪花对象
	PrintMsg(snowflake.SnowInit(1))
	global.Log.Info("Snow init successfully")

	routers.InitRouter(global.Cfg)
}
